module.exports = {
	minitips:0,
	rolecolor:1,//1=红方，2=蓝方
	gametype:0,//游戏类型：1=人机单机对战（默认玩家红旗先手）,2=人人实时对战(猜拳决定先手)
	logicqipanarr:[],//逻辑棋盘二维数组
	
	//打乱qizi
	randomqizi(){
		//规则：sidx是全内容的序号，百位1=红2=蓝，十位=棋子名字序号，个位为棋子的格子位置1为左边
		let allanqisidx = [
			131,132,141,142,151,152,161,162,171,172,173,174,175,
			231,232,241,242,251,252,261,262,271,272,273,274,275
		];
		let jssidx = [121,110,122,221,210,222];
		allanqisidx.sort(this.randomSort);
		return allanqisidx;
	},
	//获取所有棋子的hl位置
	getallqizihl(){
		let allazhl = [
			{h:0,l:0},{h:0,l:1},{h:0,l:2},{h:0,l:6},{h:0,l:7},{h:0,l:8},
			{h:2,l:1},{h:2,l:7},
			{h:3,l:0},{h:3,l:2},{h:3,l:4},{h:3,l:6},{h:3,l:8},
			{h:6,l:0},{h:6,l:2},{h:6,l:4},{h:6,l:6},{h:6,l:8},
			{h:7,l:1},{h:7,l:7},
			{h:9,l:0},{h:9,l:1},{h:9,l:2},{h:9,l:6},{h:9,l:7},{h:9,l:8}
		];
		let alljshl = [{h:0,l:3},{h:0,l:4},{h:0,l:5},{h:9,l:3},{h:9,l:4},{h:9,l:5}];
		return {allazhl:allazhl,alljshl:alljshl};
	},
	getposxy(hl){
		return {
			x : -308 + hl.l*77,
			y : 426 - hl.h*76
		};
	},
	//获取棋盘全部坐标
	getallpos(){
		let allpos =[],allhl=[];
		for(let h = 9;h >= 0;h--){//行
			allpos[h] = [];
			for(let l = 8;l >= 0;l--){//列
				allpos[h][l] = {
					x : -308 + l*77,
					y : -258 + h*76
				}
			}
		}
		/*
		let allpos = [
			{h:0,l:0},{h:0,l:1},{h:0,l:2},{h:0,l:3},{h:0,l:4},{h:0,l:5},{h:0,l:6},{h:0,l:7},{h:0,l:8},
			{h:1,l:0},{h:1,l:1},{h:1,l:2},{h:1,l:3},{h:1,l:4},{h:1,l:5},{h:1,l:6},{h:1,l:7},{h:1,l:8},
			{h:2,l:0},{h:2,l:1},{h:2,l:2},{h:2,l:3},{h:2,l:4},{h:2,l:5},{h:2,l:6},{h:2,l:7},{h:2,l:8},
			{h:3,l:0},{h:3,l:1},{h:3,l:2},{h:3,l:3},{h:3,l:4},{h:3,l:5},{h:3,l:6},{h:3,l:7},{h:3,l:8},
			{h:4,l:0},{h:4,l:1},{h:4,l:2},{h:4,l:3},{h:4,l:4},{h:4,l:5},{h:4,l:6},{h:4,l:7},{h:4,l:8},
			{h:5,l:0},{h:5,l:1},{h:5,l:2},{h:5,l:3},{h:5,l:4},{h:5,l:5},{h:5,l:6},{h:5,l:7},{h:5,l:8},
			{h:6,l:0},{h:6,l:1},{h:6,l:2},{h:6,l:3},{h:6,l:4},{h:6,l:5},{h:6,l:6},{h:6,l:7},{h:6,l:8},
			{h:7,l:0},{h:7,l:1},{h:7,l:2},{h:7,l:3},{h:7,l:4},{h:7,l:5},{h:7,l:6},{h:7,l:7},{h:7,l:8},
			{h:8,l:0},{h:8,l:1},{h:8,l:2},{h:8,l:3},{h:8,l:4},{h:8,l:5},{h:8,l:6},{h:8,l:7},{h:8,l:8},
			{h:9,l:0},{h:9,l:1},{h:9,l:2},{h:9,l:3},{h:9,l:4},{h:9,l:5},{h:9,l:6},{h:9,l:7},{h:9,l:8}

		];
		*/
		return {allpos:allpos,allhl:allhl};
	},
	//逻辑棋盘初始化（零化）
	qipanarrinit(){
		this.logicqipanarr =[];
		for(let h = 9;h >= 0;h--){//行
			this.logicqipanarr[h] = [];
			for(let l = 8;l >= 0;l--){//列
				this.logicqipanarr[h][l] = {sidx:0,h:h,l:l,show:0};
			}
		}
	},
	//初始化逻辑棋盘
	initlogicqipan(){
		let allanqisidx = this.randomqizi();
		cc.log('g:allanqisidx:',JSON.stringify(allanqisidx));
		let allazhl = this.getallqizihl().allazhl;
		let jspos = this.caiquan(this.rolecolor);
		cc.log('g:jspos:',JSON.stringify(jspos));
		//分布暗子
		for(let i = allazhl.length-1;i >= 0;i--){
			let h = allazhl[i].h,l = allazhl[i].l;
			//cc.log('g:分布暗子,i=',i,',=',JSON.stringify(allanqisidx[i]));
			this.logicqipanarr[h][l] = {sidx:allanqisidx[i],h:h,l:l,show:0};
		}
		//分布将士
		for(let j = 5;j >= 0;j--){
			let h = jspos[j].h,l = jspos[j].l;
			this.logicqipanarr[h][l] = {sidx:jspos[j].sidx,h:h,l:l,show:1};
		}
		cc.log('g:initlogicqipan,qp:',JSON.stringify(this.logicqipanarr));
	},
	//根据猜拳结果决定红方蓝方，红方先手
	caiquan(cqresult){
		let jspos =[{sidx:221,h:0,l:3},{sidx:210,h:0,l:4},{sidx:222,h:0,l:5},
					{sidx:121,h:9,l:3},{sidx:110,h:9,l:4},{sidx:122,h:9,l:5}];
		if(cqresult == 1){
			cc.log('qp:caiquan:猜拳结果red');
		}else if(cqresult == 2){
			for(let i = 2;i >= 0;i--){
				jspos[i].h = 9;
				jspos[i+3].h = 0;
			}
			cc.log('qp:caiquan:猜拳结果blue');
		}else{
			cc.director.emit('alert','猜拳结果有误，需要重新猜拳');
			cc.log('qp:caiquan:猜拳结果有误，需要重新猜拳');
		}
		cc.log('qp:caiquan:猜拳:',cqresult);
		return jspos;
	},
	//棋子的属性，红蓝，名字
	qiziatt(sidx){
		//规则：sidx是全内容的序号，百位1=红2=蓝，十位=棋子名字序号，个位为棋子的格子位置1为左边
		/*110,121,122,131,132,141,142,151,152,161,162,171,172,173,174,175,
		210,221,222,231,232,241,242,251,252,261,262,271,272,273,274,275
		*/
		//cc.log('g:sidx:',sidx,',of=',typeof(sidx));
		
		let qzatt = {};
		qzatt.color = parseInt(sidx/100);//百位
		qzatt.gezi = parseInt(sidx%10);//个位
		let qztype = parseInt((sidx/10)%10);//十位
		qzatt.name = '';
		//cc.log('lei:',parseInt(sidx[1]));
		//cc.log('g:qzatt.move:',qzatt.move,',of=',typeof(parseInt(sidx[1])));
		switch(qztype){
			case 1:
				if(qzatt.color == 1){
					qzatt.name = '帅';
				}else if(qzatt.color == 2){
					qzatt.name = '将';
				}
				//qzatt.move = '';
				break;
			case 2:
				if(qzatt.color == 1){
					qzatt.name = '仕';
				}else if(qzatt.color == 2){
					qzatt.name = '士';
				}
				break;
			case 3:
				if(qzatt.color == 1){
					qzatt.name = '相';
				}else if(qzatt.color == 2){
					qzatt.name = '象';
				}
				break;
			case 4:
				if(qzatt.color == 1){
					qzatt.name = '馬';
				}else if(qzatt.color == 2){
					qzatt.name = '马';
				}
				break;
			case 5:
				if(qzatt.color == 1){
					qzatt.name = '車';
				}else if(qzatt.color == 2){
					qzatt.name = '车';
				}
				break;
			case 6:
				if(qzatt.color == 1){
					qzatt.name = '炮';
				}else if(qzatt.color == 2){
					qzatt.name = '砲';
				}
				break;
			case 7:
				if(qzatt.color == 1){
					qzatt.name = '兵';
				}else if(qzatt.color == 2){
					qzatt.name = '卒';
				}
				break;
			default:
				break;
		}
		return qzatt;
	},

	//乱序
	randomSort(a, b) { 
		return Math.random() > 0.5 ? -1 : 1; 
	},
	//获取棋子的所有可移动的点
 	movearr(qzcell){//只给出相对棋子本身的移动参数,{sidx:221,h:0,l:3,show:1}
		let qztype = parseInt((qzcell.sidx/10)%10);
		let color = parseInt(qzcell.sidx/100);
		let movearr = [];//
		//只能动自己的兵
		if(this.rolecolor != color){
			return false;	
		}
		switch(qztype){
			case 1://将帅
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					movearr[movearr.length] = {offl:0,offh:1};
					movearr[movearr.length] = {offl:0,offh:-1};
					movearr[movearr.length] = {offl:-1,offh:0};
					movearr[movearr.length] = {offl:-1,offh:0};
				}
				break;
			case 2://士
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					movearr[movearr.length] = {offl:1,offh:1};
					movearr[movearr.length] = {offl:1,offh:-1};
					movearr[movearr.length] = {offl:-1,offh:1};
					movearr[movearr.length] = {offl:-1,offh:-1};
				}
				break;
			case 3://相
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					movearr[movearr.length] = {offl:2,offh:2};//相脚{offl:1,offh:1}
					movearr[movearr.length] = {offl:2,offh:-2};//相脚{offl:1,offh:-1}
					movearr[movearr.length] = {offl:-2,offh:2};//相脚{offl:-1,offh:1}
					movearr[movearr.length] = {offl:-2,offh:-2};//相脚{offl:-1,offh:-1}
				}
				break;
			case 4://马
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					movearr[movearr.length] = {offl:1,offh:2};//马脚{offl:0,offh:1}
					movearr[movearr.length] = {offl:1,offh:-2};//马脚{offl:0,offh:-1}
					movearr[movearr.length] = {offl:-1,offh:2};//马脚{offl:0,offh:1}
					movearr[movearr.length] = {offl:-1,offh:-2};//马脚{offl:0,offh:-1}
					movearr[movearr.length] = {offl:2,offh:1};//马脚{offl:1,offh:0}
					movearr[movearr.length] = {offl:2,offh:-1};//马脚{offl:1,offh:0}
					movearr[movearr.length] = {offl:-2,offh:1};//马脚{offl:-1,offh:0}
					movearr[movearr.length] = {offl:-2,offh:-1};//马脚{offl:-1,offh:0}
				}
				break;
			case 5://车
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					//对某列加入可移动的点
					for(let h = 9;h >= 0;h--){
						if(h != qzcell.h){
							movearr[movearr.length] = {offl:0,offh:h-qzcell.h};
						}
					}
					//对某列加入可移动的点
					for(let l = 9;l >= 0;l--){
						if(l != qzcell.l){
							movearr[movearr.length] = {offl:l-qzcell.l,offh:0};
						}
					}
				}
				break;
			case 6://炮
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					//对某列加入可移动的点
					for(let h = 9;h >= 0;h--){
						if(h != qzcell.h){
							movearr[movearr.length] = {offl:0,offh:h-qzcell.h};
						}
					}
					//对某行加入可移动的点
					for(let l = 9;l >= 0;l--){
						if(l != qzcell.l){
							movearr[movearr.length] = {offl:l-qzcell.l,offh:0};
						}
					}
				}
				break;
			case 7://兵
				if(this.rolecolor == color){//玩家和棋子同色就代表棋子是自己这边的
					if(qzcell.h >= 5){
						movearr[movearr.length] = {offl:0,offh:-1};
					}else{
						movearr[movearr.length] = {offl:0,offh:-1};
						movearr[movearr.length] = {offl:-1,offh:0};
						movearr[movearr.length] = {offl:1,offh:0};
					}
				}
				break;
			default:
				break;
		}
		return movearr;
	},
	//显示可移动可吃的范畴==此处规则还不完整，缺乏相的不能过河设定，显示可移动点也有问题
	canmoveeat(qzinfo,canmove){//qzinfo:{sidx:221,h:0,l:3,show:1},canmove:{offl:0,offh:1}
		let newhl={h:0,l:0,canmove:1,caneat:0};
		let qztype = parseInt((qzinfo.sidx/10)%10);
		newhl.h = qzinfo.h + canmove.offh;
		newhl.l = qzinfo.l + canmove.offl;
		if(newhl.h > 10 || newhl.l > 9){
			return;
		}
		cc.log('g-qzinfo:',JSON.stringify(qzinfo));
		cc.log('g-canmove:',JSON.stringify(canmove));
		cc.log('g-newhl.h:',newhl.h,',newhl.l=',newhl.l);
		//目标点
		let point = this.logicqipanarr[newhl.h][newhl.l];
		cc.log('g-point:',JSON.stringify(point));
		let color = parseInt(point.sidx/100);
		//碍脚点
		let adh=0,adl=0;
		if(newhl.h > 0){
			adh = newhl.h -1;
		}else{
			adh = newhl.h +1;
		}
		if(newhl.l > 0){
			adl = newhl.l -1;
		}else{
			adl = newhl.l +1;
		}
		let aidian = this.logicqipanarr[adh][adl];	
		switch(qztype){
			case 1://将帅
				if(point.sidx > 0){//目标点有棋子
					newhl.canmove = 0;
					if(color != this.rolecolor){
						newhl.caneat = 1;
					}
				}
				break;
			case 2://士
				if(point.sidx > 0){//目标点有棋子
					newhl.canmove = 0;
					if(color != this.rolecolor){
						newhl.caneat = 1;
					}
				}
				break;
			case 3://相
				if(aidian.sidx > 0){//碍脚点有棋子
					newhl.canmove = 0;
				}else{
					if(point.sidx > 0){//目标点有棋子
						newhl.canmove = 0;
						if(color != this.rolecolor){
							newhl.caneat = 1;
						}
					}
				}
				break;
			case 4://马
				if(aidian.sidx > 0){//碍脚点有棋子
					newhl.canmove = 0;
				}else{
					if(point.sidx > 0){//目标点有棋子
						newhl.canmove = 0;
						if(color != this.rolecolor){
							newhl.caneat = 1;
						}
					}
				}
				break;
			case 5://车,车跟目标点之间有棋子就不能移动
				if(canmove.offl == 0){//列不动行动
					let count = 0;
					if(canmove.offh > 0){
						for(let i = canmove.offh;i >= 1;i--){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
								if(point.sidx > 0){//目标点有棋子
									newhl.canmove = 0;
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}else{
						for(let i = canmove.offh;i <= -1;i++){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
								if(point.sidx > 0){//目标点有棋子
									newhl.canmove = 0;
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}
				}
				if(canmove.offh == 0){//行不动列动
					let count = 0;
					if(canmove.offl > 0){
						for(let i = canmove.offl;i >= 1;i--){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
								if(point.sidx > 0){//目标点有棋子
									newhl.canmove = 0;
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}else{
						for(let i = canmove.offl;i <= -1;i++){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
								if(point.sidx > 0){//目标点有棋子
									newhl.canmove = 0;
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}
				}
				break;
			case 6://炮
				if(canmove.offl == 0){//列不动行动
					let count = 0;
					if(canmove.offh > 0){
						for(let i = canmove.offh;i >= 1;i--){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
							}
							if(i == 1 && count == 1){
								if(point.sidx > 0){//目标点有棋子
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}else{
						for(let i = canmove.offh;i <= -1;i++){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
							}
							if(i == 1 && count == 1){
								if(point.sidx > 0){//目标点有棋子
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}
				}
				if(canmove.offh == 0){//行不动列动
					let count = 0;
					if(canmove.offl > 0){
						for(let i = canmove.offl;i >= 1;i--){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
								if(point.sidx > 0){//目标点有棋子
									newhl.canmove = 0;
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}else{
						for(let i = canmove.offl;i <= -1;i++){
							let guiji = this.logicqipanarr[qzinfo.h + i][qzinfo.l];
							if(guiji.sidx > 0){//目标点有棋子
								count++;
							}
							if(i == 1 && count == 0){
								newhl.canmove = 0;
								if(point.sidx > 0){//目标点有棋子
									newhl.canmove = 0;
									if(color != this.rolecolor){
										newhl.caneat = 1;
									}
								}
							}
						}
					}
				} 
				break;
			case 7://兵
				if(point.sidx > 0){//目标点有棋子
					newhl.canmove = 0;
					if(color != this.rolecolor){
						newhl.caneat = 1;
					}
				}
				break;
			default:
				break;
		}
		return newhl;
	}
}